#include "scene.h"

#include "utils/commands.h"
#include "src/utils/events.h"

#include "src/gui/exitpanel.h"
#include "src/gui/enginesettingspanel.h"

Scene::Scene(int argc, char* argv[])
{
	Cdcommands::Instance()->scene = this;
 	Events::Instance()->cmd = Cdcommands::Instance();
// 	Events::Instance()->set(this, Cdcommands::Instance());

	// register SETTINGS
		settings->registerCVar("option",			0, 0, 10, true,		"this is an option");
		settings->doCommandLineOptions(1, argc, argv);

	// register PANELS
	 	canvas.addWidgetPanel( "panel_exit", 			new Exitpanel() );
	 	canvas.addWidgetPanel( "panel_enginesettings", 		new Enginesettingspanel() );
		canvas.setDefaultZAxis();

		
	// register COMMANDS
		Cdcommands::Instance()->registerCmd("gui_togglepanel", 	&Scene::gui_swapChild);
		Cdcommands::Instance()->registerCmd("testprint", 	&Scene::testprint);

	// register EVENTS tied to commands
	 	Events::Instance()->registerEvent(SDLK_ESCAPE,		"gui_togglepanel-panel_exit", 			execcmd.gen("gui_togglepanel", "panel_exit"), 0, 0, 0 );
	 	Events::Instance()->registerEvent(SDLK_F1,		"gui_togglepanel-panel_enginesettings", 	execcmd.gen("gui_togglepanel", "panel_enginesettings"), 0, 0, 0 );
	 	Events::Instance()->registerEvent(SDLK_t,		"testprint", 					execcmd.gen("testprint"), 0, 0, 0 );

	// shortcut pointers
// 		headless = settings->getCVarPtr("headless");
}

void Scene::draw()
{
	frameCounter++;

	Timer::Instance()->mark();
// 	world->sleeper.mark();
	Events::Instance()->handlecommands();

	if ( !*headless )
	{
		glDisable(GL_DEPTH_TEST);
		glClear(GL_COLOR_BUFFER_BIT);
// 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// 	cerr << "frame" << endl;

		if ( canvas.active )
		{
			glViewport(0,0,*settings->winWidth,*settings->winHeight);  // this is a simple camera

			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			glOrtho(0, *settings->winWidth, *settings->winHeight, 0,  0, 1);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

// 			glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
    
			glPushMatrix();
			glTranslatef(0.5f, -0.5f, 0.0f); // pixel precision offset
			canvas.draw();

			glPopMatrix();
		}
		SDL_GL_SwapBuffers();
	}
}

void Scene::testprint()
{
	cerr << "hello bitch" << endl;
}

Scene::~Scene()
{
}
